package com.woniuxy.demo.controller;

import com.woniuxy.examentity.ExamScoreResult;
import com.woniuxy.examentity.ExamSubmission;
import com.woniuxy.demo.server.impl.AIScoringService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/aichat")
@CrossOrigin
public class AiChatController {
    private final ChatClient chatClient;
    private final AIScoringService aiScoringService;

    public AiChatController(ChatClient chatClient, AIScoringService aiScoringService) {
        this.chatClient = chatClient;
        this.aiScoringService = aiScoringService;
    }

    @GetMapping("/getask")
    public String getAsk() {
        String prompt = """
                我是一名Java初学者，目前在学习反射，
                随机给我生成一套试卷，每次题都不一样，
                五个选择，三个简答，两个编程，给我json格式。
                选择题需要包含题目、选项数组、答案索引(0开始)、解析；简答题和编程题需要包含题目、答案。
                所有题目放在questions数组中，每个题目有type字段标识(choice/essay/programming)和id字段，
                选择题选项数组放在options，每个答案放在answer里，解析放在analysis里
                """;
        return chatClient.prompt()
                .user(prompt)
                .call()
                .content();
    }

    @PostMapping("/getscore")
    public ResponseEntity<ExamScoreResult> getScore(@RequestBody ExamSubmission submission) {
        try {
            // 处理评分逻辑
            ExamScoreResult result = aiScoringService.scoreExam(submission);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}
